const exec = (<any>window).require('child_process').exec;
const fs = (<any>window).require('fs');
const path = (<any>window).require('path');
export interface IDir {
    desk?: string;
    name: string;
    isDir: boolean;
    fullPath: string;
    isClose?: boolean;
    size?: number;
    children: IDir[];
}
export function showLetter(callback: { (res: string[]): void }): Promise<string[]> {
    return new Promise(resolve => {
        let wmicResult;
        let command = exec('wmic logicaldisk get caption', function (err, stdout, stderr) {
            if (err || stderr) {
                return;
            }
            wmicResult = stdout;
        });
        command.stdin.end();
        command.on('close', function (code) {
            var data = wmicResult.split('\n'), result = {};
            let res: string[] = [];
            for (let item of data) {
                if (item.indexOf(':') != -1) {
                    res.push((<string>item).substring(0, item.indexOf(':')));
                }
            }
            callback(res);
            resolve(res);
        });
    });
}
export function readFileList(dir, filesList = [], rootPath: string, deepth = 0) {
    if(deepth != 0) {
        deepth--;
    }else{
        return filesList;
    }
    const files = fs.readdirSync(dir);
    files.forEach((item, index) => {
        var fullPath = path.join(dir, item);
        let temp = {
            name: '',
            isDir: true,
            fullPath: fullPath,
            size: 0,
            children: []
        }
        try {
            const stat = fs.statSync(fullPath);
            if (stat.isDirectory()) {
                temp.name = item,
                temp.isDir = true;      
                readFileList(path.join(dir, item), temp.children, rootPath, deepth);  //递归读取文件
            } else {                
                temp.isDir = false;
                temp.name = item;
                temp.size = stat.size
            }
            filesList.push(temp);              
        } catch (error) {
            
        }
        // temp.fullPath = path.join(dir, item).replace(rootPath, '');
             
    });
    return filesList;
}
export function orderFile(dirs: IDir[], r = (dir: IDir) => {}) {
    let dir: IDir[] = [];
    let file: IDir[] = [];
    for(let i = 0; i < dirs.length;i++) {
      r(dirs[i]);
      if(dirs[i].isDir == true) {
        dirs[i].children = orderFile(dirs[i].children, r);
        dir.push(dirs[i]);
      }else{
        file.push(dirs[i]);
      }
    }
    return dir.concat(file);
  }